package com.yunlong.redis;

import com.yunlong.common.TokenInfo;
import com.yunlong.constant.RedisKeyTemplate;
import com.yunlong.util.RedisUtil;

import com.yunlong.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;


/**
 * @author ycSong
 */
@Component
public class RedisRepositoryImpl implements RedisRepository {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void saveMessageCode(String phone, String messageCode) {
        RedisUtil.set(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_VERIFICATION_CODE, phone), messageCode);
    }

    /**
     * 保存邮箱和验证码的键值对<email, messageCode>
     *
     * @param email
     * @param messageCode 验证码
     */
    @Override
    public void saveEmailMessageCode(String email, String messageCode) {
        RedisUtil.set(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_VERIFICATION_CODE, email), messageCode);
    }

    @Override
    public String selectMessageCodeByPhone(String phone) {
        return RedisUtil.<String>get(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_VERIFICATION_CODE, phone), String.class);
    }

    /**
     * 根据key：手机号 查询验证码
     *
     * @param email
     * @return
     */
    @Override
    public String selectMessageCodeByEmail(String email) {
        return RedisUtil.<String>get(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_VERIFICATION_CODE, email), String.class);
    }

    @Override
    public void deleteMessageCode(String phone) {
        RedisUtil.del(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_VERIFICATION_CODE, phone));
    }

    @Override
    public void saveAccessToken(TokenInfo tokenInfo) {
//        "TOKEN:%s"
//        "TOKEN:Token"
        RedisUtil.set(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_ACCESS_TOKEN, tokenInfo.getAccessToken()), tokenInfo.getUserId());
    }

    @Override
    public void saveLoginAccessToken(TokenInfo tokenInfo) {
//        "USER_TOKEN:%s"
//        "USER_TOKEN:UserId"
        RedisUtil.set(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_USER_CURRENT_TOKEN, tokenInfo.getUserId()), tokenInfo.getAccessToken());
    }

    /**
     * 认证，即保存一个完整的键值对
     * @param tokenInfo
     * "MANAGER:Token"
     */
    @Override
    public void saveManagerAccessToken(TokenInfo tokenInfo) {
        RedisUtil.set(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_MANAGER_ACCESS_TOKEN, tokenInfo.getAccessToken()), tokenInfo.getUserId());
    }

    /**
     * 登录认证，即保存一个完整的键值对
     * "MANAGER_TOKEN:managerId"
     * @param tokenInfo
     */
    @Override
    public void saveManagerLoginAccessToken(TokenInfo tokenInfo) {
        RedisUtil.set(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_MANAGER_CURRENT_TOKEN, tokenInfo.getUserId()), tokenInfo.getAccessToken());
    }

    @Override

    public String selectAccessToken(String accessToken) {
        //    "TOKEN:%s"
        return RedisUtil.<String>get(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_ACCESS_TOKEN, accessToken), String.class);
    }

    /**
     * 查询认证,通过Token查Id
     *
     * @param userId
     * @return
     */
    @Override
    public String selectUserToken(String userId) {
        return RedisUtil.<String>get(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_USER_CURRENT_TOKEN, userId), String.class);
    }

    /**
     * 根据id查询managerToken
     * @param managerId
     * @return
     */
    @Override
    public String selectManagerToken(String managerId) {
//        "MANAGER_TOKEN:%s"
//        "MANAGER_TOKEN:managerId"
        return RedisUtil.<String>get(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_MANAGER_CURRENT_TOKEN, managerId), String.class);
    }

    /**
     * 选取管理员token
     *
     * @param accessToken "MANAGER_TOKEN:managerId"
     * @return
     */
    @Override
    public String selectManagerId(String accessToken) {
        return RedisUtil.<String>get(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_MANAGER_ACCESS_TOKEN, accessToken), String.class);
    }

    /**
     * 删除管理员认证-通过token删除
     *RedisKeyTemplate.T_MANAGER_TOKEN为管理员ID
     * @param accessToken 加冒号是为了创建文件夹
     * "MANAGER:%s"
     * "MANAGER:accessToken"
     */
    @Override
    public void deleteManagerToken(String accessToken) {
        RedisUtil.del(redisTemplate,RedisKeyUtil.buildKey(RedisKeyTemplate.T_MANAGER_ACCESS_TOKEN,accessToken));
    }

    /**
     * 删除管理员认证-通过ManagerID删除
     *"MANAGER_TOKEN:managerId"
     * @param managerId
     */
    @Override
    public void deleteLoginManagerToken(String managerId) {
        RedisUtil.del(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_MANAGER_CURRENT_TOKEN, managerId));
    }

    @Override
    public String selectLoginAccessToken(String userId) {
//        USER_TOKEN:%s
//        USER_TOKEN:userId
        return RedisUtil.<String>get(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_USER_CURRENT_TOKEN, userId), String.class);
    }

    @Override
    public void deleteAccessToken(String accessToken) {
//        "TOKEN:%s"
//        "TOKEN:Token"
        RedisUtil.del(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_ACCESS_TOKEN, accessToken));
    }

    @Override
    public void deleteLoginAccessToken(String userId) {
        RedisUtil.del(redisTemplate, RedisKeyUtil.buildKey(RedisKeyTemplate.T_USER_CURRENT_TOKEN, userId));
    }


}
